package de.tabuma.lib.cm.crud;

import de.tabuma.lib.contentmanagement.*;
import de.tabuma.lib.contentmanagement.entity.Content;
import javax.servlet.http.*;
import de.tabuma.lib.*;
import java.lang.reflect.*;
import java.util.logging.*;

/**
 *
 * @author alefen
 */
public class RemoveViewFacade {

    private static final Logger log = Logger.getLogger(RemoveViewFacade.class.getName());

    public static String view(Content content, HttpServletRequest request) {
        String str = "";
        try {
            String remove = "remove";
            String cancel = "cancel";

            Object object = request.getAttribute(constants.OBJECT);

            String list = "";
            boolean first = false;
            for(int i=0;i<content.FormContent.getEditableFields().size();i++) {
                Field myField = content.FormContent.getEditableFields().get(i);
                boolean add = true;
                if(content.FormContent.getEditableFieldTypes().get(i)==constants.TYPE_PASSWORD) {
                    add = false;
                }
                if(content.FormContent.getEditableFieldTypes().get(i)==constants.TYPE_TEXTAREA) {
                    add = false;
                }
                if(add) {
                    if(first) {
                        list += ", ";
                    }
                    first = true;
                    String get = ObjectMethods.getGetter(myField.getName());
                    list += object.getClass().getMethod(get).invoke(object);
                }
            }

            String question = "Do you really want to remove "+content.FormContent.getEntityName()+"(" + list + ") ?";

            Integer intId = (Integer) request.getAttribute(content.GeneralContent.getParamName() + constants.id);

            if(request.getAttribute(constants.MESSAGE_ERROR)!=null) {
                str += "<div class=\"errormessage\"><div class=\"errormessage2\">"+request.getAttribute(constants.MESSAGE_ERROR)+"</div></div>\n";
            }
            if(request.getAttribute(constants.MESSAGE_INFO)!=null) {
                str += "<div class=\"infomessage\"><div class=\"infomessage2\">"+request.getAttribute(constants.MESSAGE_INFO)+"</div></div>\n";
            }

            str += "<div class=\"contentContent\">";
            str += "<p>" + question + "</p>";
            str += view.getFormBeginn(content.GeneralContent.getServlet(), null);
            str += view.getHiddenAsAttribute(content.GeneralContent.getParameter());
            str += view.printHiddenField(content.GeneralContent.getParamName(), ""+constants.REMOVE);
            str += view.printHiddenField(constants.REMOVE1, "true");
            str += view.printHiddenField(content.GeneralContent.getParamName()+constants.id, ""+intId);
            str += "<input type=\"submit\" name=\"submit\" class=\"button\" value=\""+remove+" "+content.FormContent.getEntityName()+"\"/>";
            str += "<input type=\"submit\" name=\"" + constants.CANCEL + "\" class=\"button\" value=\""+cancel+"\"/>";
            str += view.getFormEnd();
            str += "</div>";
        }
        catch(Exception e) {
            String warning = e.getMessage()+"/"+e.getCause()+"/"+e.getClass()+"\n";
            for(int i=0;i<e.getStackTrace().length;i++) {
                warning += e.getStackTrace()[i]+"\n";
            }
            log.warning(warning);
        }
        return str;
    }
}